package co.dreamapps.dodoattack.model;

import java.util.Random;

import org.andengine.entity.IEntity;
import org.andengine.entity.scene.Scene;
import co.dreamapps.dodoattack.activities.GameActivity;

public class CornCreator {

	/**
	 * Creator attributes
	 */
	private GameState gameState;
	private GameActivity gameActivity;
	private IEntity dodoEntity;
	private Scene scene;
	private int xBorn;
	private int xTarget, yTarget;

	/**
	 * Constants
	 */
	private static final int WAIT_TIME = 25;
	private static final int ANIMATION_TIME = 18;
	private static final int TIME_DIVISION = WAIT_TIME + ANIMATION_TIME;
	private static final int MAX_PATH_CHOISE_DIFFERENCE = 2;
	private static final int NUMBER_OF_PATHS = 4;

	/**
	 * Variables
	 */
	private int pathCounter[];
	private int chosenPath;
	private int maxCounter;
	private int counter;

	/**
	 * Connection
	 */
	RoomManagerDodoGame roomManager;

	public CornCreator(RoomManagerDodoGame roomManager, GameState gameState, GameActivity gameActivity, IEntity dodoEntity, Scene scene, int xBorn, int xTarget, int yTarget) {
		this.roomManager = roomManager;
		this.gameState = gameState;
		this.gameActivity = gameActivity;
		this.dodoEntity = dodoEntity;
		this.scene = scene;
		this.xBorn = xBorn;
		this.xTarget = xTarget;
		this.yTarget = yTarget;

		this.pathCounter = new int[NUMBER_OF_PATHS];

	}

	public void work() {
		counter ++;
		counter %= TIME_DIVISION;
		if(roomManager != null && counter == 0)
			choosePath();
		else if(counter >= WAIT_TIME) {
			switch (chosenPath) {
			case 0:
				path0(counter - WAIT_TIME);
				break;
			case 1:
				path1(counter - WAIT_TIME);
				break;
			case 2:
				path2(counter - WAIT_TIME);
				break;
			case 3:
				path3(counter - WAIT_TIME);
				break;
			}
		} else
			sendUpdates();
	}

	public void choosePath() {
		Random ale = new Random();
		int min = Integer.MAX_VALUE;
		for (int i = 0; i < pathCounter.length; i++) {
			if(pathCounter[i] < min)
				min = pathCounter[i];
		}
		while(true) {
			chosenPath = ale.nextInt(12345) % pathCounter.length;
			if(pathCounter[chosenPath] < min + MAX_PATH_CHOISE_DIFFERENCE)
				break;
		}
		pathCounter[chosenPath] ++;
		if(pathCounter[chosenPath] > maxCounter) {
			maxCounter = pathCounter[chosenPath];
		}
	}
	
	public void choosePath(int counter, int chosenPath) {
		this.counter = counter;
		this.chosenPath = chosenPath;
	}

	public void path0(int c) {
		if(c % 2 == 0) {
			if(c < ANIMATION_TIME/2) {
				Corn corn = new Corn(gameActivity, gameState);
				corn.addCornOnScene(gameActivity.getVertexBufferObjectManager(), scene, xBorn + 10, 200 + c*30, xTarget, yTarget, dodoEntity);
			} else {
				c -= ANIMATION_TIME/2;
				Corn corn = new Corn(gameActivity, gameState);
				corn.addCornOnScene(gameActivity.getVertexBufferObjectManager(), scene, xBorn + 10, 400 - c*30, xTarget, yTarget, dodoEntity);
			}
		}
	}

	public void path1(int c) {
		if(c % 2 == 0) {
			Corn corn = new Corn(gameActivity, gameState);
			corn.addCornOnScene(gameActivity.getVertexBufferObjectManager(), scene, xBorn + 10, 200, xTarget, yTarget, dodoEntity);
		}
	}

	public void path2(int c) {
		if(c % 2 == 0) {
			Corn corn = new Corn(gameActivity, gameState);
			corn.addCornOnScene(gameActivity.getVertexBufferObjectManager(), scene, xBorn + 10, 400, xTarget, yTarget, dodoEntity);
		}
	}

	public void path3(int c) {
		if(c < 3) {
			Corn corn1 = new Corn(gameActivity, gameState);
			corn1.addCornOnScene(gameActivity.getVertexBufferObjectManager(), scene, xBorn + 10, 200 + c*160, xTarget, yTarget, dodoEntity);
			Corn corn2 = new Corn(gameActivity, gameState);
			corn2.addCornOnScene(gameActivity.getVertexBufferObjectManager(), scene, xBorn + 35, 200 + c*160 + 80, xTarget, yTarget, dodoEntity);
		}
	}

	private void sendUpdates() {
		if(roomManager != null && counter % 3 == 0)
			roomManager.cornChoise(counter, chosenPath);
	}

}
